package com.sgb.kjwl.viewmodel.machine;

import android.text.TextUtils;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.user.PublicMyPriceEntity;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.H5ReEntity;

public class PublicMyPriceListModel extends ViewModel {

    public MutableLiveData<PublicMyPriceEntity> showListLiveData = new MutableLiveData<>();//报价列表
    public MutableLiveData<String> showStateLiveData = new MutableLiveData<>();//状态变更成功
    public MutableLiveData<String> showMyUrlLiveData = new MutableLiveData<>();//后台H5详情URL
    public MutableLiveData<String[]> showUrlLiveData = new MutableLiveData<>();//前台H5详情URL

    private int page = 1;
    private int size = 20;
    public boolean hasNext = false;//是否有下一页

    //获取租赁经营———我的报价--列表数据(获取列表首页)
    public void getDataList(String isAcceptQuote, String categoryType, String titleOrCompanyName, String titleOrDemandCompanyName, String type, boolean isLoadMore) {
        page = isLoadMore ? page + 1 : 1;
        APIManager.getInstance().getLeaseAPI().getPackersFinanceList(page, size, isAcceptQuote, categoryType, titleOrCompanyName, titleOrDemandCompanyName, type)
                .enqueue(new ICallBack<BaseEntity<PublicMyPriceEntity>>(true, true) {
                    @Override
                    public void onSuccess(BaseEntity<PublicMyPriceEntity> data) {
                        showListLiveData.postValue(data.getData());
                    }
                });

    }

//    //更改报价
//
//    public void changePrice(String id, String price, String remark, String quoteUtil, String categoryType) {
//        DialogHelper.showProgressDialog(myPriceListFragment.getContext(), null, "更改报价中...", 0, false, null).setCanceledOnTouchOutside(false);
//        page++;
//        Map map = new HashMap();
//        map.put("rentalSeekId", id);
//        map.put("quoteUtil", quoteUtil);
////        map.put("quoteKey", quoteKey);
//        map.put("quotePrice", price);
//        map.put("remark", remark);
//        map.put("categoryType", categoryType);//机械，1设备 2车辆，3房产，4土地 5工厂 6农机
//        APIManager.getInstance().getMachineAPI().changMyQuote(map).enqueue(new Callback<BaseEntity>() {
//            @Override
//            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
//                DialogHelper.dismissProgressDialog();
//                if (response.body() == null || response.body().getState() == null) {
//                    return;
//                }
//                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
//                    //更改报价成功
//                    myPriceListFragment.showDelSuccToast("更改报价成功...");
//
//                    myPriceListFragment.initData(false);//更改成功刷新页面
//
//                } else {
//                    MToast.showToast(myPriceListFragment.getActivity(), response.body().getMsg());
//                }
//            }
//
//            @Override
//            public void onFailure(Call<BaseEntity> call, Throwable t) {
//                DialogHelper.dismissProgressDialog();
//                ToastUtils.show(myPriceListFragment.getContext(), "更改报价失败");
//            }
//        });
//    }

//    //删除报价
//    public void delPrice(String id) {
//        APIManager.getInstance().getLeaseAPI().deleteMyQuote(id).enqueue(new ICallBack<BaseEntity>(true, true) {
//            @Override
//            public void onSuccess(BaseEntity data) {
//                showStateLiveData.postValue("删除报价成功");
//            }
//        });
//    }

    //取消报价
    public void cancelPrice(String id) {
        APIManager.getInstance().getMachineAPI().cancelQuote(id).enqueue(new ICallBack<BaseEntity>(true, true) {
            @Override
            public void onSuccess(BaseEntity data) {
                showStateLiveData.postValue("取消报价成功");
            }
        });
    }

    //接受报价
    public void acceptPrice(String rentalSeekId, String id, String categoryType) {
        APIManager.getInstance().getMachineAPI().acceptOffer(rentalSeekId, id, categoryType).enqueue(new ICallBack<BaseEntity>(true, true) {
            @Override
            public void onSuccess(BaseEntity data) {
                showStateLiveData.postValue("接受报价成功");
            }
        });
    }

    //忽略报价
    private void ignorePrice(String id, String time, String categoryType) {
        APIManager.getInstance().getMachineAPI().ignoreMatch(id, time, categoryType).enqueue(new ICallBack<BaseEntity>(true, true) {
            @Override
            public void onSuccess(BaseEntity data) {
                showStateLiveData.postValue("忽略报价成功");
            }
        });

    }

    public void getMyH5Url(String categoryType, String id) {
        String urlName = "";
        if ("0".equals(categoryType)) {//机械
            urlName = "mechanicsoffer";
        } else if ("1".equals(categoryType)) {//设备
            urlName = "equipmentoffer";
        } else if ("2".equals(categoryType)) {//车辆
            urlName = "vehicleoffer";
        } else if ("3".equals(categoryType)) {//房产
            urlName = "houseoffer";
        } else if ("4".equals(categoryType)) {//土地
            urlName = "landOfferDetails";
        } else if ("5".equals(categoryType)) {//工厂
            urlName = "factoryOfferDetails";
        } else if ("6".equals(categoryType)) {//农机
            urlName = "farmOfferDetails";
        }
        APIManager.getInstance().getLeaseAPI().getUrlSetting(urlName).enqueue(new ICallBack<BaseEntity<H5ReEntity>>(true, true) {
            @Override
            public void onSuccess(BaseEntity<H5ReEntity> data) {
                String url = data.getData().getUrl() + "?id=" + id;
                showMyUrlLiveData.postValue(url);
//                WebViewX5HostActivity.start(myPriceListFragment.getContext(), url);
            }
        });
    }

    public void getH5Url(String categoryType, String id) {
        String urlName = "";
        if ("0".equals(categoryType)) {//机械
            urlName = "mechanicsRentDetails";
        } else if ("1".equals(categoryType)) {//设备
            urlName = "equipmentRentDetails";
        } else if ("2".equals(categoryType)) {//车辆
            urlName = "vehicleRentDetails";
        } else if ("3".equals(categoryType)) {//房产
            urlName = "houseRentDetails";
        } else if ("4".equals(categoryType)) {//土地
            urlName = "landRentDetails";
        } else if ("5".equals(categoryType)) {//工厂
            urlName = "factoryRentDetails";
        } else if ("6".equals(categoryType)) {//农机
            urlName = "farmRentDetails";
        }
        if (TextUtils.isEmpty(urlName)) {
            return;
        }
        APIManager.getInstance().getPickAPI().getUrlSetting(urlName).enqueue(new ICallBack<BaseEntity<H5ReEntity>>(true, true) {
            @Override
            public void onSuccess(BaseEntity<H5ReEntity> data) {
                String url = data.getData().getUrl() + "?id=" + id;
                showUrlLiveData.postValue(new String[]{url, id, String.valueOf(Integer.parseInt(categoryType) + 1)});
//                WebViewX5HostActivity.start(askLeaseFragment.getActivity(), url, id, "asklease", categoryType);
            }
        });
    }

}
